package com.ww.word.ui;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ww.core.activity.MyActivity;
import com.ww.core.util.DeviceUtil;
import com.ww.core.util.ImageUtil;
import com.ww.core.util.TimeUtils;
import com.ww.word.R;
import com.ww.word.ui.paint.BigSize;
import com.ww.word.ui.paint.Colours;
import com.ww.word.ui.paint.MyHorizontalScrollView;
import com.ww.word.ui.paint.OnScrollListener;
import com.ww.word.ui.paint.TuyaView;

/**
 * @since 
 * @version 2013-12-12
 * @author wangwei
 */
public class TuyaActivity extends MyActivity implements OnClickListener, OnScrollListener {

	private TuyaView tuyaView;
	private FrameLayout tuyaFrameLayout = null;

	private static final int UNDO_PATH = 1;
	private static final int REDO_PATH = 2;
	private static final int USE_ERASER = 3;
	private static final int USE_PAINT = 4;

	//
	private LinearLayout linearlayout;
	private Button colourtag;
	private Button bigtag;
	private TextView time, txt;
	private ScrollView scrollviewcolour;
	private ScrollView scrollviewbig;

	private MyHorizontalScrollView hscrollViewcolour;
	private MyHorizontalScrollView hscrollViewsize;

	private List<Colours> colours = new ArrayList<Colours>();
	private Colours colour;
	private List<BigSize> sizes = new ArrayList<BigSize>();
	private BigSize size;
	private int index;
	//
	private int CANCLE_BACKGROUND_IMAGE = 0;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UNDO_PATH:
				int undo = tuyaView.undo(); //撤销上次操作
				System.out.println("可以撤销：" + undo);
				if (undo < 0) {
					CANCLE_BACKGROUND_IMAGE++;
					switch (CANCLE_BACKGROUND_IMAGE) {
					case 0:
						break;
					case 1:
						CANCLE_BACKGROUND_IMAGE = 0;
						break;
					}
				}

				break;
			case REDO_PATH:
				int redo = tuyaView.clean(); //返回上次操作
				System.out.println("可以前进：" + redo);
				if (redo < 1) {
					//设置按钮不可用
				}
				break;
			case USE_ERASER:
				if (linearlayout.getVisibility() == View.VISIBLE) {
					linearlayout.setVisibility(View.GONE);
				}
				TuyaView.color = Color.parseColor("#C9DDFE");
				TuyaView.srokeWidth = 15;
				break;
			case USE_PAINT:
				if (linearlayout.getVisibility() == View.GONE) {
					linearlayout.setVisibility(View.VISIBLE);
					TuyaView.srokeWidth = sizes.get(index).getName() + 10;
					for (int i = 0; i < colours.size(); i++) {
						if (colours.get(i).getButtonbg().getVisibility() == View.VISIBLE) {
							TuyaView.color = Color.parseColor("#" + colours.get(i).getName());
							break;
						}
					}
				} else {
					linearlayout.setVisibility(View.GONE);
				}
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tuya_layout);
		setTitle(R.string.practise_title);
		tuyaFrameLayout = (FrameLayout) findViewById(R.id.tuya_layout);
		tuyaView = (TuyaView) findViewById(R.id.tuyaView);
		colourtag = (Button) this.findViewById(R.id.colourtag);
		bigtag = (Button) this.findViewById(R.id.bigtag);
		time = (TextView) this.findViewById(R.id.time);
		txt = (TextView) this.findViewById(R.id.txt);
		scrollviewcolour = (ScrollView) this.findViewById(R.id.scrollviewcolour);
		scrollviewbig = (ScrollView) this.findViewById(R.id.scrollviewbig);
		hscrollViewcolour = (MyHorizontalScrollView) this.findViewById(R.id.HorizontalScrollView01);
		hscrollViewcolour.setOnScrollListener(this);
		hscrollViewsize = (MyHorizontalScrollView) this.findViewById(R.id.HorizontalScrollView02);
		hscrollViewsize.setOnScrollListener(this);
		linearlayout = (LinearLayout) this.findViewById(R.id.ScrollView01);
		txt.setText(getIntent().getStringExtra("name"));
		ss();
		initColourButton();
	}

	private void ss() {
		TextPaint tp2 = txt.getPaint();
		//字体
		//		 txt.setTypeface(Typeface.createFromAsset(this.getAssets(), "zy.3gp"));
		tp2.setFakeBoldText(true);
		//设置光源的方向
		float[] direction = new float[] { 1, 1, 1 };
		//设置环境光亮度
		float light = 0.4f;
		//选择要应用的反射等级
		float specular = 6;
		//向mask应用一定级别的模糊
		float blur = 3.5f;
		EmbossMaskFilter maskfilter = new EmbossMaskFilter(direction, light, specular, blur);
		tp2.setMaskFilter(maskfilter);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (linearlayout.getVisibility() == View.VISIBLE) {
				linearlayout.setVisibility(View.GONE);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_changebg:
			Message redo_message = new Message();
			redo_message.what = REDO_PATH;
			handler.sendMessage(redo_message);
			break;
		case R.id.button_pen:
			Message pen_message = new Message();
			pen_message.what = USE_PAINT;
			handler.sendMessage(pen_message);
			break;
		case R.id.colourtag:
			if (linearlayout.getVisibility() == View.VISIBLE) {
				scrollviewcolour.setVisibility(View.VISIBLE);
				scrollviewbig.setVisibility(View.GONE);
				colourtag.setBackgroundResource(R.drawable.tuya_selectedtrue);
				bigtag.setBackgroundResource(R.drawable.tuya_selectedfalse);
			}
			break;
		case R.id.bigtag:
			if (linearlayout.getVisibility() == View.VISIBLE) {
				scrollviewcolour.setVisibility(View.GONE);
				scrollviewbig.setVisibility(View.VISIBLE);
				bigtag.setBackgroundResource(R.drawable.tuya_selectedtrue);
				colourtag.setBackgroundResource(R.drawable.tuya_selectedfalse);
			}
			break;
		case R.id.sendbutton:
			sendTuyaBitmap();
			break;
		}
		//颜色
		int j = -1;
		for (int i = 0; i < colours.size(); i++) {
			if (v.getId() == colours.get(i).getTag()) {
				j = i;
			}
		}
		if (j != -1) {
			for (int i = 0; i < colours.size(); i++) {
				colours.get(i).getButtonbg().setVisibility(View.INVISIBLE);
			}
			colours.get(j).getButtonbg().setVisibility(View.VISIBLE);
			//改变颜色
			colours.get(j).getName();
			TuyaView.color = Color.parseColor("#" + colours.get(j).getName());
			return;
		}

		//大小
		for (int i = 0; i < sizes.size(); i++) {
			if (v.getId() == sizes.get(i).getTag()) {
				j = i;
			}
		}
		if (j != -1) {
			for (int i = 0; i < sizes.size(); i++) {
				sizes.get(i).getButton().setBackgroundResource(0);
			}
			sizes.get(j).getButton().setBackgroundResource(R.drawable.tuya_brushsizeselectedbg);
			//改变大小
			sizes.get(j).getName();
			TuyaView.srokeWidth = sizes.get(j).getName() + 10;
			index = j;
			return;
		}
	}

	/**
	 * 发送图片
	 */
	private void sendTuyaBitmap() {
		//发送按钮
		boolean drawingCacheEnabled = tuyaFrameLayout.isDrawingCacheEnabled();
		time.setText(TimeUtils.getNowTime());
		if (!drawingCacheEnabled) {
			tuyaFrameLayout.setDrawingCacheEnabled(true);
			tuyaFrameLayout.buildDrawingCache();
		}
		//获得当前的Bitmap对象
		Bitmap bitmap = tuyaFrameLayout.getDrawingCache();
		if (bitmap != null) {
			if (DeviceUtil.checkSdcard(this)) {
				ImageUtil.saveBitmap(bitmap, ImageUtil.IMAGEPATH, txt.getText().toString() + TimeUtils.getNowTime()
						+ ".jpg");
				showMessage(R.string.practise_success);
			} else {
				showMessage(R.string.practise_sdcard);
			}
		} else {
			showMessage(R.string.practise_fail);
		}
	}

	@Override
	public void onLeft() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRight() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll() {
		// TODO Auto-generated method stub

	}

	private void initColourButton() {
		// TODO Auto-generated method stub
		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button01));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview01));
		colour.setName("140c09");
		colour.setTag(R.id.button01);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button02));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview02));
		colour.setName("fe0000");
		colour.setTag(R.id.button02);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button03));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview03));
		colour.setName("ff00ea");
		colour.setTag(R.id.button03);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button04));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview04));
		colour.setName("011eff");
		colour.setTag(R.id.button04);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button05));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview05));
		colour.setName("00ccff");
		colour.setTag(R.id.button05);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button06));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview06));
		colour.setName("00641c");
		colour.setTag(R.id.button06);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button07));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview07));
		colour.setName("9bff69");
		colour.setTag(R.id.button07);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button08));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview08));
		colour.setName("f0ff00");
		colour.setTag(R.id.button08);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button09));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview09));
		colour.setName("ff9c00");
		colour.setTag(R.id.button09);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button10));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview10));
		colour.setName("ff5090");
		colour.setTag(R.id.button10);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button11));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview11));
		colour.setName("9e9e9e");
		colour.setTag(R.id.button11);
		colours.add(colour);

		colour = new Colours();
		colour.setButton((Button) this.findViewById(R.id.button12));
		colour.setButtonbg((ImageView) this.findViewById(R.id.imageview12));
		colour.setName("f5f5f5");
		colour.setTag(R.id.button12);
		colours.add(colour);

		for (int i = 0; i < colours.size(); i++) {
			colours.get(i).getButton().setOnClickListener(this);
			colours.get(i).getButtonbg().setVisibility(View.INVISIBLE);
		}
		colours.get(1).getButtonbg().setVisibility(View.VISIBLE);

		size = new BigSize();
		size.setButton((Button) this.findViewById(R.id.sizebutton01));
		size.setName(15);
		size.setTag(R.id.sizebutton01);
		sizes.add(size);
		size = new BigSize();
		size.setButton((Button) this.findViewById(R.id.sizebutton02));
		size.setName(10);
		size.setTag(R.id.sizebutton02);
		sizes.add(size);
		size = new BigSize();
		size.setButton((Button) this.findViewById(R.id.sizebutton03));
		size.setName(5);
		size.setTag(R.id.sizebutton03);
		sizes.add(size);
		size = new BigSize();
		size.setButton((Button) this.findViewById(R.id.sizebutton04));
		size.setName(0);
		size.setTag(R.id.sizebutton04);
		sizes.add(size);
		size = new BigSize();
		size.setButton((Button) this.findViewById(R.id.sizebutton05));
		size.setName(-5);
		size.setTag(R.id.sizebutton05);
		sizes.add(size);
		size = new BigSize();
		size.setButton((Button) this.findViewById(R.id.sizebutton06));
		size.setName(-10);
		size.setTag(R.id.sizebutton06);
		sizes.add(size);
		for (int i = 0; i < sizes.size(); i++) {
			sizes.get(i).getButton().setOnClickListener(this);
			sizes.get(i).getButton().setBackgroundResource(0);
		}
		sizes.get(2).getButton().setBackgroundResource(R.drawable.tuya_brushsizeselectedbg);
		index = 2;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		TuyaView.color = Color.parseColor("#fe0000");
		TuyaView.srokeWidth = 15;
	}
}